//
//  FJNumberKeyboard.m
//  OnionHome
//
//  Created by fengjie on 2019/10/21.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "FJNumberKeyboard.h"
#import "EMKeyboardButton.h"
#import "UIImage+EMColor.h"
#import "UIResponder+EMFirstResponder.h"


@implementation FJNumberKeyboard

#pragma mark - init

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kEMKeyboardViewBackgroundColor;
        [self createKeyBoard];

    }
    return self;
}


#pragma mark - private

- (void)createKeyBoard
{
    int col = 4;
    NSArray *keyBoards = @[@(1),@(2),@(3),@(EMKeyboardButtonTypeDelete),@(4),@(5),@(6),@(EMKeyboardButtonTypeComplete),@(7),@(8),@(9),@(EMKeyboardButtonTypeNone),@(0),@(EMKeyboardButtonTypeNone),@(EMKeyboardButtonTypeDecimal),@(EMKeyboardButtonTypeNone)];
            
    CGFloat spacing = 8;
    CGFloat btnWidth = (kEMScreenWidth - 5 * spacing ) / col;
    
    
    for (int i = 0; i < keyBoards.count; i++) {
        NSInteger keyBoardType = [[keyBoards objectAtIndex:i] integerValue];
        EMKeyboardButton *btn = [EMKeyboardButton buttonWithType:UIButtonTypeCustom];
        
        CGFloat X = i % col * btnWidth + (i % col + 1)* spacing;
        CGFloat Y = i / col * kEMNumberKeyboardBtnHeight + (i / col + 1)* spacing;
        
        btn.frame = CGRectMake( X ,Y, btnWidth, kEMNumberKeyboardBtnHeight);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:btn];
        
        if (keyBoardType < EMKeyboardButtonTypeNone) {
            btn.KeyboardButtonType = EMKeyboardButtonTypeNumber;
            [btn setTitle:[NSString stringWithFormat:@"%zd",keyBoardType] forState:UIControlStateNormal];
        }else{
            btn.KeyboardButtonType = keyBoardType;
        }
        DLog(@"%ld",(long)keyBoardType);
        //普通数字键盘特殊处理
        if (keyBoardType == EMKeyboardButtonTypeNone){
            btn.frame = CGRectZero;
        }
        if (keyBoardType == 0) {
            btn.frame = CGRectMake( X, Y, btnWidth * 2 + spacing, kEMNumberKeyboardBtnHeight);
        }
        if (keyBoardType == EMKeyboardButtonTypeComplete) {
            btn.frame = CGRectMake(X, Y + 2 * spacing, btnWidth , kEMNumberKeyboardBtnHeight * 3);
            [btn setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
            [btn setTitleColor:KWhiteColor forState:UIControlStateNormal];
            btn.titleLabel.font = PingFangSC_Semibold(20);
            if (StrValid(self.completeTitle)) {
                [btn setTitle:self.completeTitle forState:UIControlStateNormal];
            }
            self.completeBtn = btn;
        }
        
        ViewRadius(btn, 4);
        
    }
}
-(void)setCompleteTitle:(NSString *)completeTitle{
    _completeTitle = completeTitle;
    [self.completeBtn setTitle:completeTitle forState:UIControlStateNormal];
}

/**
 点击

 @param sender btn
 */
- (void)btnClick:(EMKeyboardButton *)sender
{
    
    UIView <UITextInput> *textView = [UIResponder firstResponderTextView];
    switch (sender.KeyboardButtonType) {
            
        case EMKeyboardButtonTypeNumber:
        case EMKeyboardButtonTypeDecimal:
        case EMKeyboardButtonTypeStockHeader00:
        case EMKeyboardButtonTypeStockHeader600:
        case EMKeyboardButtonTypeStockHeader601:
        case EMKeyboardButtonTypeStockHeader300:
        case EMKeyboardButtonTypeStockHeader000:
        case EMKeyboardButtonTypeStockHeader002:{
            [self inputNumber:sender.titleLabel.text];
        }
            break;
            
            
        case EMKeyboardButtonTypeDelete:{
            [textView deleteBackward];
        }
            break;
            
        case EMKeyboardButtonTypeComplete:{
//            [textView resignFirstResponder];
            BlockSafeRun(self.backBlock);
        }
            break;
            
        case EMKeyboardButtonTypeResign:{
            [textView resignFirstResponder];
        }
            break;
            
        case EMKeyboardButtonTypeABC:{
            //切换到字母键盘
//            if (self.changeASCIIBlock) {
//                self.changeASCIIBlock();
//            }
        }
            break;
            
        case EMKeyboardButtonTypeStockPositionFull:
        case EMKeyboardButtonTypeStockPositionHalf:
        case EMKeyboardButtonTypeStockPositionOneThird:
        case EMKeyboardButtonTypeStockPositionQuartern:{
//            if (self.stockPositionBtnClickBlock) {
//                self.stockPositionBtnClickBlock(sender.KeyboardButtonType);
//            }
        }
            break;
            
        default:
            break;
    }
}
/**
 输入文字

 @param text text
 */
- (void)inputNumber:(NSString *)text
{
    [UIResponder inputText:text];
}

@end
